<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>Header
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 左侧导航栏 -->
    <el-container>
      <el-aside :width="toggcollapse?'64px':'200px'">
        <!-- 折叠菜单栏 -->
        <div class="toggle-button" @click="togglecollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="toggcollapse"
          :collapse-transition="false"
          router
          :default-active="avtivepath"
        >
          <!-- 一级菜单栏 -->
          <el-submenu :index="''+item.id" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <!-- 字体图标 -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单栏 -->
            <el-menu-item
              :index="'/'+itemchi.path"
              v-for="itemchi in item.children"
              :key="itemchi.id"
              @click="activepaths('/'+itemchi.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{itemchi.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      // 字体图标
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 菜单栏是否折叠
      toggcollapse: false,
      // 被激活的链接
      activepath: ''
    }
  },
  created() {
    this.getMenuList()
    this.avtivepath = window.sessionStorage.getItem('avtivepath')
  },
  methods: {
    loginout: function() {
      window.sessionStorage.clear('token')
      this.$router.push('/login')
    },
    // 获取菜单项渲染视图
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)

      if (res.meta.status !== 200) {
        return this.$message.error('获取菜单列表失败')
      }
      this.menulist = res.data
      console.log(this.menulist)
    },
    // 切换菜单栏展开
    togglecollapse() {
      this.toggcollapse = !this.toggcollapse
      console.log(1111)
    },
    // 切换链接的激活状态
    activepaths(avtivepath) {
      window.sessionStorage.setItem('avtivepath', avtivepath)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>